package indexPack;

import java.io.File;
import java.io.IOException;
import javax.swing.text.BadLocationException;

import org.apache.lucene.analysis.Analyzer;

import tools.CustomizedFilter;
import directoryPath.CustomizedDirectory;
import java.util.ArrayList;


public class IndexManager {

    private CustomizedIndex index;
    private CustomizedDirectory indexDir;
    private CustomizedDirectory dataDir;
    private String fileType;
    private Analyzer analyzer;

    public IndexManager(CustomizedDirectory indexDir, CustomizedDirectory dataDir, Analyzer analyzer,
            String fileType) {
        
        this.indexDir = indexDir;
        this.dataDir = dataDir;
        this.analyzer = analyzer;
        this.fileType = fileType;
    }

    public ArrayList<String> createIndex() throws IOException, BadLocationException {
        
        this.index = new CustomizedIndex(indexDir.getFile(), dataDir.getFile(), analyzer);
        File[] files = dataDir.getListFiles();
        CustomizedFilter filter = new CustomizedFilter();
        filter.setFileType(fileType);
        // Vector con la lista de archivos que no pudieron ser indexados para mostrarlos
        ArrayList<String> notIndexed = new ArrayList<String>();
        boolean indexResult;
        
        for(File file : files) {
            if (filter.accept(file)) {
                indexResult = this.index.indexFile(file);
                if (!indexResult) {
                    notIndexed.add(file.getName() + "\n");
                }
            }
        }
        this.index.optimize();
        this.index.close();
        
        return notIndexed;  // Se devuelve la lista de objetos que no se pudieron indexar
    }

    public void setIndexDir(CustomizedDirectory cd) {
        this.indexDir = cd;
    }

    public CustomizedDirectory getIndexDir() {
        return this.indexDir;
    }

    public void setDataDir(CustomizedDirectory cd) {
        this.dataDir = cd;
    }

    public CustomizedDirectory getDataDir() {
        return dataDir;
    }

    public void setFileType(String ft) {
        this.fileType = ft;
    }

    public String getFileType() {
        return fileType;
    }

    public void setAnalyzer(Analyzer a) {
        this.analyzer = a;
    }

    public Analyzer getAnalyzer() {
        return analyzer;
    }

}
